package org.billsnow.android.deliverydriver;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.SimpleAdapter;

public class ShiftListActivity extends ListActivity {
  
  private static ArrayList<Shift> shifts;
  
  
  private static final String[] PROJECTION = new String[] {
    //ShiftProvider._ID,
    ShiftProvider.START,
    ShiftProvider.END,
    ShiftProvider.WAGE,
    ShiftProvider.NRUNS,
    ShiftProvider.TIPSC,
    ShiftProvider.TIPSCC,
    ShiftProvider.BONUS,
    ShiftProvider.MILES
  };
  
  private static final int MAPSIZE = 6;
  private static final String[] KEYS = new String[] {
    "period", "length", "takehome", "nruns", "wages", "miles"
  };
  
  private static final int[] SHIFTVIEW = new int[] {
    R.id.time,
    R.id.hrsworked,
    R.id.takehome,
    R.id.shiftruns,
    R.id.wages,
    R.id.milage
  };
  
  private class ShiftMap<K,V> extends HashMap<K,V> {
    /**
     * Auto-generated by eclipse
     */
    private static final long serialVersionUID = 4688478512015212748L;    
    
    public ShiftMap(K[] keys, V[] values, int n) {
      super(n);
      for (int i=0; i<n; i++) {
        super.put(keys[i], values[i]);
      }
    }
  }
  
  private Uri getID() {
    
  }
  
  private Shift getShift(Cursor c) {
    /*if (c.isNull(c.getColumnIndex(ShiftProvider.MILES))) {
      return new Shift(c.getLong(c.getColumnIndex(ShiftProvider.START)),
                       c.getLong(c.getColumnIndex(ShiftProvider.END)),
                       c.getInt(c.getColumnIndex(ShiftProvider.WAGE)),
                       c.getInt(c.getColumnIndex(ShiftProvider.NRUNS)),
                       c.getInt(c.getColumnIndex(ShiftProvider.TIPSC)),
                       c.getInt(c.getColumnIndex(ShiftProvider.TIPSCC)),
                       c.getInt(c.getColumnIndex(ShiftProvider.BONUS)),
                       null);
    }
    else {*/
      return new Shift(c.getLong(c.getColumnIndex(ShiftProvider.START)),
                       c.getLong(c.getColumnIndex(ShiftProvider.END)),
                       c.getInt(c.getColumnIndex(ShiftProvider.WAGE)),
                       c.getInt(c.getColumnIndex(ShiftProvider.NRUNS)),
                       c.getInt(c.getColumnIndex(ShiftProvider.TIPSC)),
                       c.getInt(c.getColumnIndex(ShiftProvider.TIPSCC)),
                       c.getInt(c.getColumnIndex(ShiftProvider.BONUS)),
                       c.getFloat(c.getColumnIndex(ShiftProvider.MILES)));
    
  }
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Calendar lastweek = Calendar.getInstance();
    lastweek.add(Calendar.WEEK_OF_MONTH, -1);
    Cursor c = managedQuery(ShiftProvider.CONTENT_URI, PROJECTION, 
                            ShiftProvider.START + ">" + String.valueOf(lastweek.getTimeInMillis()), 
                            null, ShiftProvider.START);
    shifts = new ArrayList<Shift>(c.getCount());
    for (int i=0; c.moveToPosition(i); i++) {
      shifts.add(i, getShift(c));
    }
  }
  
  @Override
  protected void onStart() {
    Intent intent = getIntent();
    if (intent.getAction() == Intent.ACTION_VIEW) {
      Cursor c = managedQuery(intent.getData(), PROJECTION, null, null, null);
      if (c.moveToFirst()) {
        shifts.add(getShift(c));
      }
    }
    ArrayList<ShiftMap<String,String>> map = new ArrayList<ShiftMap<String,String>>(shifts.size());
    for (int i=0; i<shifts.size(); i++) {
      String[] values = new String[] {
        shifts.get(i).getShiftTime(), shifts.get(i).getShiftLength(),
        shifts.get(i).getTakeHome(), shifts.get(i).getNRunsReadable(),
        shifts.get(i).getWageReadable(), "N/A"
      };
      map.add(i, new ShiftMap<String,String>(KEYS, values, MAPSIZE));
    }
    SimpleAdapter adapter = new SimpleAdapter(this, map, R.layout.shift, KEYS, SHIFTVIEW);
    setListAdapter(adapter);
  }
}
